import { isMobile } from "@/utils/validate";
import includeJs from "@/utils/include-js";

export default {
	data() {
		return {
			tplload: false,
			order_sn: '',
			mobile_firest_template: {},
			mobile_second_template: {},
			pc_firest_template: {},
			tpl: {}
		}
	},
	methods: {
		async getTplInfo(id, type = 1) {
			const { query } = this.$route;
			this.tplload = true
			const { mobile_firest_template, mobile_second_template, pc_firest_template, order_sn } =
				await this.$api
					.getAdView({ ...query, id, type })
					.catch((e) => {
						this.$toast({
							message: e,
							position: "top"
						});
						throw e;
					})
					.finally(() => {
						this.tplload = false;
					});
			this.order_sn = order_sn;
			this.mobile_firest_template = mobile_firest_template;
			this.mobile_second_template = mobile_second_template;
			this.pc_firest_template = pc_firest_template;
			let tpl = isMobile ? mobile_firest_template : pc_firest_template;
			this.tpl = tpl;

			const { behavior } = tpl;
			for (const item of behavior) {
				if (item.behavior_monitor && item.behavior_copy) {
					// includeJs.exec(item.behavior_monitor);
					includeJs.source(item.behavior_monitor);
				}
			}
			return tpl
		},
		behavior(type = 'copy', cb) {
			const { behavior } = this.tpl;
			for (const item of behavior) {
				if (item.behavior_monitor && item['behavior_' + type]) {
					includeJs.exec(item['behavior_' + type]);
				}
			}
			cb && cb()
		}
	}
}
